import Axios from 'axios';
import qs from 'qs';
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
const setuuid = require('uuid')


const dev = '/api/wx'
const prod = '/api2/wx'


 const instand = Axios.create({
    timeout:5000})
    instand.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

  if (process.env.NODE_ENV === 'development') {
    instand.defaults.baseURL = dev
  } else if (process.env.NODE_ENV === 'debug') {
    instand.defaults.baseURL = ''
  } else if (process.env.NODE_ENV === 'production') {
    instand.defaults.baseURL = prod
  }
    
instand.interceptors.request.use(data =>{
  let token = sessionStorage.getItem('token') || ''
  data.headers.token = token
  let uuid = localStorage.getItem('uuid') || ''
  if(!uuid){
    localStorage.setItem('uuid',setuuid.v4())
  }
   
  data.headers.uuid = localStorage.getItem('uuid')
  
  nprogress.start()
  if(data.method == 'post'){
    data.params = qs.stringify(data.params)
  }
  return data
},err=>{
  return Promise.reject(err)

})
instand.interceptors.response.use(data =>{
  nprogress.done()
  if(data.status === 200){
    return data.data
  }
  
},err=>{
  return Promise.reject(err)

})

export const request = instand


